#include "userinformation.h"
#include "ui_userinformation.h"

#include <QSqlQuery>
#include <QMessageBox>
#include "adminsystem.h"

UserInformation::UserInformation(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::UserInformation)
{
    ui->setupUi(this);

    model = new QSqlTableModel(this);
    model->setTable("user");
    model->select();
    ui->tableView->setModel(model);
}

UserInformation::~UserInformation()
{
    delete ui;
}

void UserInformation::on_pushButtonOK_clicked()
{
    if(ui->lineEditUserId->text().isEmpty())
    {
        QMessageBox::warning(this,NULL,"Account is entry");
        return;
    }

    if(!ui->radioButtonDownOrdinaryUser->isChecked() && !ui->radioButtonUpAdministrator->isChecked()
            && !ui->radioButtonUpBookEntryUser->isChecked())
    {
        QMessageBox::warning(this,NULL,"Not be selected");
        return;
    }

    model->setFilter(tr("Account = '%1'").arg(ui->lineEditUserId->text()));
    QSqlQuery query;
    QString str = ui->lineEditUserId->text();

    if(ui->radioButtonUpAdministrator->isChecked())
    {
        query.exec("UPDATE user SET UserRight = 0 WHERE Account = '"+str+"'");

        ui->lineEditUserId->clear();
        model->select();
        model->submitAll();

    }
    else if(ui->radioButtonUpBookEntryUser->isChecked())
    {
        query.exec("UPDATE user SET UserRight = 1 WHERE Account = '"+str+"'");
        ui->lineEditUserId->clear();
        model->select();
        model->submitAll();
    }
    else if(ui->radioButtonDownOrdinaryUser->isChecked())
    {
        query.exec("update user set UserRight = 2 where Account = '"+str+"'");
        ui->lineEditUserId->clear();
        model->select();
        model->submitAll();
    }

}

void UserInformation::on_pushButtonUpdate_clicked()
{
    model->setTable("user");
    model->select();
    ui->tableView->setModel(model);
}


void UserInformation::on_pushButton_clicked()
{
    close();
}

void UserInformation::on_pushButtonReturnAdminsystem_clicked()
{
    AdminSystem *as = new AdminSystem;
    as->show();
    close();
}
